local basic = require("luaScript.module.common.basic");
local config = require("luaScript.module.config.redis-config");
local redis = require("resty.redis");

local poolSize = config.pool_size;
local poolMaxIdleTime = config.pool_max_idle_time;

local _Module = {};
_Module.__index = _Module;

-- new操作
function _Module.new(self)
    local cls = {
        red = nil
    };
    setmetatable(cls, self);
    return cls;
end

-- 打开连接
function _Module.open(self)
    local redisObj = redis:new();
    redisObj:set_timeout(config.timeout, config.timeout, config.timeout);
    local ok, err = redisObj:connect(config.host_name, config.port);
    if not ok then
        basic.error("连接redis服务器失败: ", err)
        return false;
    end

    if config.password then
        redisObj:auth(config.password);
    end
    if config.db then
        redisObj:select(config.db);
    end

    basic.log("连接redis服务器成功");

    self.red = redisObj;
    return true;
end

-- 缓存值
function _Module.setValue(self, key, value)
    local ok, err = self.red:set(key, value);
    if not ok then
        basic.error("redis 缓存设置失败");
        return false;
    end
    basic.log("set result ok")
    return true;
end

--值递增
function _Module.incrValue(self, key)
    local ok, err = self.red:incr(key)
    if not ok then
        basic.error("redis 缓存递增失败 ")
        return false;
    end
    basic.log("incr ok")
    return ok;
end

--过期
function _Module.expire(self, key, seconds)
    local ok, err = self.red:expire(key, seconds)
    if not ok then
        basic.log("redis 设置过期失败 ")
        return false;
    end
    return true;
end

-- 获取某个值
function _Module.getValue(self, key)
    local resp, err = self.red:get(key);
    if not resp then
        basic.log("redis get失败 ");
        return nil;
    end
    return resp;
end

-- 操作set集合的
function _Module.getSmembers(self, key)
    local resp, err = self.red:smembers(key);
    if not resp then
        basic.log("redis 缓存读取失败 ")
        return nil;
    end
    return resp;
end

-- hash set值
function _Module.hsetValue(self, key, hkey, value)
    local ok, err = self.red:hset(key, hkey, value);
    if not ok then
        basic.error("redis 缓存设置失败");
        return false;
    end
    basic.log("set result ok")
    return true;
end

--获取值
function _Module.hgetValue(self, key, id)
    local resp, err = self.red:hget(key, id)
    if not resp then
        basic.log("redis hget 失败 ")
        return nil;
    end
    return resp;
end

-- 执行脚本
function _Module.evalsha(self, sha, key1, key2)
    local resp, err = self.red:evalsha(sha, key1, key2);
    if not resp then
        basic.log("redis evalsha 执行失败 ")
        return nil;
    end
    basic.log("set result ok")
    return resp;
end

-- 获取连接
function _Module.getConnection(self)
    return self.red;
end

-- 回收连接
function _Module.close(self)
    if not self.red then
        return;
    end
    -- 该方法将当前的Redis连接立即放入连接池
    -- 如果入池成功，则返回1；如果入池出现错误，则返回nil
    -- 下一次通过red:connect(…)获取连接时，connect方法在创建新连接前，首先会在连接池中查找空闲连接，只有查找失败时才会真正创建新连接
    local ok, err = self.red:set_keepalive(poolMaxIdleTime, poolSize);
    if not ok then
        basic.log("keepalive执行失败");
        return false, "keepalive执行失败";
    end

    basic.log("redis 连接释放成功");
    return true, "redis 连接释放成功";
end

return _Module;